package com.tedu.bbs.controller;

import com.tedu.bbs.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Controller
public class ArticleController {
    @RequestMapping("/articleList")
    public void articleList(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理文章列表！");
        String uid = request.getParameter("userId");
        String username = request.getParameter("username");
        System.out.println("该用户的id是："+uid);

        try(
                Connection connection =DBUtil.getConnection();
                ){
            String sql="SELECT title " +
                    "FROM article " +
                    "WHERE u_id=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            int uid1= Integer.parseInt(uid);
            ps.setInt(1,uid1);
            ResultSet rs = ps.executeQuery();

            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("    <meta charset=\"UTF-8\">");
            pw.println("    <title>"+ username +"</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("    <h1>" + username + "的文章</h1>");
            pw.println("    <table border=\"1\">");
            pw.println("        <tr>");
            pw.println("            <td>标题</td>");
            pw.println("            <td>作者</td>");
            pw.println("        </tr>");
            while(rs.next()) {
                pw.println("             <tr>");
                pw.println("                <td>" + rs.getString("title") + "</td>");
                pw.println("                <td>" + username + "</td>");
                pw.println("            </tr>");
            }
            pw.println("        </table");
            pw.println("    </center>");
            pw.println("</body>");
            pw.println("</html>");

            rs.close();
            ps.close();

        }catch(Exception e){
            e.printStackTrace();
        }



    }


    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        if(title==null||title.isEmpty()||
           author==null||author.trim().isEmpty()||
           content==null||content.isEmpty()
        ){
            response.sendRedirect("/art_info_error.html");
        }

        try(
                Connection connention = DBUtil.getConnection();
             ){
            String sql ="SELECT id " +
                    "FROM userinfo " +
                    "WHERE username=?";
            PreparedStatement ps = connention.prepareStatement(sql);
            ps.setString(1,author);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                int id = rs.getInt("id");
                String sql1 ="INSERT INTO article(title,content,u_id) VALUES (?,?,?)";
                PreparedStatement ps1 = connention.prepareStatement(sql1);
                ps1.setString(1,title);
                ps1.setString(2,content);
                ps1.setInt(3,id);
                int num = ps1.executeUpdate();
                if(num>0){
                    response.sendRedirect("/art_success.html");
                    return;
                }else {
                    response.sendRedirect("/art_fail.html");
                }
            }
                //response.sendRedirect("/art_use_exception.html");

        }catch (Exception e){
            e.printStackTrace();
        }
        response.sendRedirect("/art_user_exception.html");


    }
}
